// pages/sys/dept/list/index.js
import {
    request
} from '../../../../request';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        deptList: [],
        modalName: null
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.initData();
    },
    initData() {
        request("/system/dept/list", "GET").then(resopnse => {
            console.log(resopnse)
            this.setData({
                deptList: resopnse.data
            });
            console.log(this.data)
        })
    },
    showModal(e) {
        this.setData({
            modalName: e.currentTarget.dataset.target
        })
    },
    toForm(e) {
        if (e.currentTarget.dataset.target == "add") {
            wx.navigateTo({
                url: '/pages/sys/dept/form/index',
            })
        }
    },
    pageBack() {
        wx.navigateBack({
            delta: 1
        });
    }, // ListTouch触摸开始
    ListTouchStart(e) {
        this.setData({
            ListTouchStart: e.touches[0].pageX
        })
    },

    // ListTouch计算方向
    ListTouchMove(e) {
        this.setData({
            ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
        })
    },

    // ListTouch计算滚动
    ListTouchEnd(e) {
        if (this.data.ListTouchDirection == 'left') {
            this.setData({
                modalName: e.currentTarget.dataset.target
            })
        } else {
            this.setData({
                modalName: null
            })
        }
        this.setData({
            ListTouchDirection: null
        })
    },
    delete(e) {
        var that = this;
        var item = e.currentTarget.dataset['item'];
        wx.showModal({
          title: '提示',
          content: '是否删除当前部门',
          success(res) {
            if (res.confirm) {
              request("/system/dept/" + item.deptId, "DELETE").then(res => {
                if (res.code == "200") {
                  wx.showToast({
                    title: '删除成功',
                    icon: '',
                    duration: 2000,
                  })  
                  that.initData();
                } else {
                  wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 2000,
                  })
                }
              })
              
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      },
      modify(e) {
        var item = e.currentTarget.dataset['item'];
        wx.navigateTo({
          url: '/pages/sys/dept/form/index?id=' + item.deptId,
        })
      }
})